WITH FD_SITE_1 AS
  (SELECT ROW_NUMBER() OVER(ORDER BY T.CODE DESC) RN , T.ID, T.CODE, T.NAME, T.BEGIN_DA_TI, T.END_DA_TI
  FROM FD_SITE T
  WHERE 1 = 1 
<#if (CODE!) != ''>
  AND (INSTR (T.CODE, :CODE )> 0
  OR INSTR (T.NAME, :CODE )    > 0)
</#if>
<#if (DOMAIN_NA_CODE!) != ''>
  AND EXISTS
    (SELECT 1
    FROM FD_DOMAIN_NA EDN
    WHERE EDN.SITE_ID                      = T.ID
    AND (INSTR (EDN.CODE, :DOMAIN_NA_CODE )> 0
    OR INSTR (EDN.NAME, :DOMAIN_NA_CODE )  > 0)
    )
</#if>
  ), FD_SITE_2 AS
  (SELECT T.*,
    (SELECT COUNT(1) FROM FD_SITE_DE SSD WHERE SSD.ENTITY_ID = T.ID
    ) AS DEFAULT_COUNT
  FROM FD_SITE_1 T
  WHERE T.RN BETWEEN :BEGIN__ROW__NUM AND :END__ROW__NUM
  )
SELECT T.* ,
  (SELECT MAX(SD.NAME)
  FROM FD_DICTIONARY SD
  JOIN FD_DICTIONARY_TY SDT ON (SDT.ID = SD.TYPE_ID)
  WHERE SDT.CODE                       = 'CONFIRM'
  AND SD.CODE                          = DECODE(T.DEFAULT_COUNT,0,'false','true')
  ) AS DEFAULT_NA
FROM FD_SITE_2 T